﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Common.Silverlight;
using SMS.Controls.SaleSVC;
using SMS.Entities;

namespace SMS.Model
{
    public class SalesModel
    {
        ServiceProxy<SaleServiceClient> proxy = null;
        public event Action<SaleDetail> GenerateSaleDetailCompleted;
        public event Action<SaleHistory> SaveSaleHistoryCompleted;
        public event Action<decimal> OnGetCustomerBalanceCompleted;
        public event Action<SMSException> OnServiceError;
        public SalesModel()
        {
            proxy = new ServiceProxy<SaleServiceClient>("Service/SaleService.svc");
            proxy.Client.GenerateSaleDetailCompleted += new EventHandler<GenerateSaleDetailCompletedEventArgs>(Client_GenerateSaleDetailCompleted);
            proxy.Client.SaveSaleHistoryCompleted += new EventHandler<SaveSaleHistoryCompletedEventArgs>(Client_SaveSaleHistoryCompleted);
            proxy.Client.GetCustomerBalanceCompleted += new EventHandler<GetCustomerBalanceCompletedEventArgs>(Client_GetCustomerBalanceCompleted);
        }

        void Client_GetCustomerBalanceCompleted(object sender, GetCustomerBalanceCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (OnGetCustomerBalanceCompleted != null)
                    OnGetCustomerBalanceCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        void Client_SaveSaleHistoryCompleted(object sender, SaveSaleHistoryCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (SaveSaleHistoryCompleted != null)
                    SaveSaleHistoryCompleted.Invoke(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        void Client_GenerateSaleDetailCompleted(object sender, GenerateSaleDetailCompletedEventArgs e)
        {
            if (e.serviceError == null)
            {
                if (GenerateSaleDetailCompleted != null)
                    GenerateSaleDetailCompleted(e.Result);
            }
            else if (OnServiceError != null)
                OnServiceError(e.serviceError);
        }

        public void GenerateSaleDetail(string userName, int Product_Stock_ID)
        {
            proxy.Client.GenerateSaleDetailAsync(userName, Product_Stock_ID);
        }

        public void SaveSaleHistory(string userName, SaleHistory saleData)
        {
            proxy.Client.SaveSaleHistoryAsync(userName, saleData);
        }

        public void GetCustomerBalance(int CustomerId)
        {
            proxy.Client.GetCustomerBalanceAsync(App.LoggedinUserName, CustomerId);
        }
    }
}
